package com.bw.a3_27zk.login

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.a3_27zk.R
import com.bw.a3_27zk.databinding.ActivityLoginBinding
import com.bw.base.mvvm.BaseActivity

@Route(path = "/app/login/LoginActivity")
class LoginActivity : BaseActivity<ActivityLoginBinding,LoginViewModel>() {

    override fun initView() {
        val string = SPUtils.getInstance().getString("phone")
        val string1 = SPUtils.getInstance().getString("pwd")
        if (string.isEmpty()&&string1.isEmpty()){

        } else{
            binding.et1.setText(string)
            binding.et2.setText(string1)
        }

        //点击跳转到注册页面
        binding.regsit.setOnClickListener{
            ARouter.getInstance().build("/app/RegisterActivity").navigation()
        }


        //点击登录

            binding.login.setOnClickListener {
                val toString = binding.et1.text.toString()
                val toString2 = binding.et2.text.toString()
                if(toString.isEmpty()||toString2.isEmpty()){
                    ToastUtils.showLong("不能为空")
                } else{
                if(!RegexUtils.isMobileSimple(toString)){
                    ToastUtils.showLong("不符合格式")
                } else{
                    val map = mutableMapOf<String,String>()
                    map.put("username",toString)
                    map.put("password",toString2)
                    viewModel.login(map)
                }

            }
        }

    }

    override fun initData() {
        viewModel.loginSuccess.observe(this){
            if (it.code == 200){
                ToastUtils.showLong("登陆成功")
                Log.d("sss", it.data.toString())
                SPUtils.getInstance().put("token",it.data.token)
            }
        }
        viewModel.loginFail.observe(this){
            ToastUtils.showLong(it)
        }
    }

    override fun getViewModel(): Class<LoginViewModel> = LoginViewModel::class.java

    override fun getLayoutId(): Int = R.layout.activity_login
}